package com.zhdl.modules.process;

import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.springContext.impl.S7ParamOptImpl;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.modules.common.logging.service.OptRecordService;
import com.zhdl.modules.common.warn.service.RunWarnService;
import com.zhdl.modules.process.BaseProcess.StatusHolder;
import com.zhdl.modules.process.service.ProjectConfigProcessService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * 此类持有项目主流程管理类 外部通过此类控制主流程 开始 暂停 急停等操作
 */
@Service
public class PLCStatusExchangeHolder {
    @Resource
    S7ParamOptImpl s7ParamOpt;
    @Resource
    OptRecordService optRecordService;
    @Resource
    ProjectConfigProcessService projectConfigProcessService;
    public StatusHolder statusHolder;
    @Resource
    RunWarnService runWarnService;
    public void checkStatus(ProjectConfiguration projectConfiguration){
        if ("初始化完成".equals(projectConfiguration.getField())){
            statusHolder.initComplete = "1".equals(projectConfiguration.getValue());
        } else  if ("自动模式".equals(projectConfiguration.getField())){
            statusHolder.setAuto( "1".equals(projectConfiguration.getValue()));
        }else  if ("停止中".equals(projectConfiguration.getField())){
            statusHolder.isStart = ( "0".equals(projectConfiguration.getValue()));
        }else  if ("总报警".equals(projectConfiguration.getField())){
            statusHolder.allAlarm = ( "1".equals(projectConfiguration.getValue()));
        }
    }

    public void init(){
        optRecordService.saveRecords("开机初始化");
        projectConfigProcessService.setExchangeBitConfigChange(this::checkStatus);
        statusHolder = new StatusHolder();
    }

    public void initLocation(){
        optRecordService.saveRecords("初始化位置");
        ProjectConfiguration value = projectConfigProcessService.getValue("初始化");
        value.setValue("1");
        DelayUtil.delayedExecution(100, () -> {
            value.setValue("0");
            s7ParamOpt.paramOpt(value,"");
        });
        s7ParamOpt.paramOpt(value,"");
    }
    public void start(){
        optRecordService.saveRecords("程序启动");
        statusHolder.isStart = true;
        ProjectConfiguration value = projectConfigProcessService.getValue("启动");
        value.setValue("1");
        DelayUtil.delayedExecution(100, () -> {
            value.setValue("0");
            s7ParamOpt.paramOpt(value,"");
        });
        s7ParamOpt.paramOpt(value,"");
    }
    public void stop() {
        ProjectConfiguration value = projectConfigProcessService.getValue("停止");
        value.setValue("1");
        s7ParamOpt.paramOpt(value,"");
        DelayUtil.delayedExecution(100, () -> {
            value.setValue("0");
            s7ParamOpt.paramOpt(value,"");
        });
        statusHolder.isStart = false;
        optRecordService.saveRecords("程序停止");
    }
    public void onPause() {
        ProjectConfiguration value = projectConfigProcessService.getValue("停止");
        value.setValue("1");
        s7ParamOpt.paramOpt(value,"");
        DelayUtil.delayedExecution(100, () -> {
            value.setValue("0");
            s7ParamOpt.paramOpt(value,"");
        });
        statusHolder.isStart = false;
        optRecordService.saveRecords("程序停止");
    }
    public void release() {

    }
    public void setAuto(boolean b) {
        optRecordService.saveRecords("手自动切换"+b);
        statusHolder.setAuto(b);
    }
    public void cancelAlarm() {
        ProjectConfiguration value = projectConfigProcessService.getValue("报警复位");
        value.setValue("1");
        s7ParamOpt.paramOpt(value,"");
        optRecordService.saveRecords("报警复位");
        DelayUtil.delayedExecution(100, () -> {
            value.setValue("0");
            s7ParamOpt.paramOpt(value,"");
        });
        runWarnService.resetAll();
    }
    public void switchTest(boolean test){

    }
}
